<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class WxUser_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_weixin_users_by_id($id){
        $query = $this->db
            ->select('*')
            ->get_where('weixin_users', array('id'=>$id));
        return $query->row_array();
    }
    public function get_weixin_users_by_openid($openid){
        $query = $this->db
            ->select('*')
            ->get_where('weixin_users', array('openid'=>$openid));
        return $query->row_array();
    }

    public function update_weixin_users_by_openid($openid,$data){
        $this->db->where('openid',$openid);
        $this->db->update('weixin_users', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }


    /**
     * @param $id
     * @param $data
     * @return bool
     * 修改微信用户信息
     */
    public function sava_weixin_users($id,$data){

        $this->db->where('id',$id);
        $this->db->update('weixin_users', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $userData
     * @return bool
     * 新增微信用户
     */
    public function subscribe($fromUser){

        $user = $this->get_weixin_users_by_openid($fromUser->openid);
        $userData = [
            'subscribe' => !empty($fromUser->subscribe) ? $fromUser->subscribe : "0",
            'openid' => $fromUser->openid,
            'nickname' => !empty($fromUser->nickname) ? $fromUser->nickname : "0",
            'sex' => !empty($fromUser->sex) ? $fromUser->sex : "0",
            'language' => !empty($fromUser->language) ? $fromUser->language : "0",
            'city' => !empty($fromUser->city) ? $fromUser->city : "0",
            'province' => !empty($fromUser->province) ? $fromUser->province : "0",
            'country' => !empty($fromUser->country) ? $fromUser->country : "0",
            'headimgurl' =>!empty($fromUser->headimgurl) ? $fromUser->headimgurl : "0",
            'subscribe_time' => !empty($fromUser->subscribe_time) ? $fromUser->subscribe_time : "0",
            'unionid'=>!empty($fromUser->unionid) ? $fromUser->unionid : "0",
        ];
        if (!empty($user)) {
            $this->sava_weixin_users($user['id'],$userData);
        } else {
            $this->db->insert('weixin_users',$userData);
        }
        if($this->db->affected_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param $userData
     * @return bool
     * 新增微信用户
     */
    public function subscribe_first($fromUser){

        $user = $this->get_weixin_users_by_openid($fromUser->openid);

        $userData = [
            'openid' => $fromUser->openid,
        ];
        if (!empty($user)) {
            return true;
        } else {
            $this->db->insert('weixin_users',$userData);
        }
        if($this->db->affected_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

    public function unsubscribe($fromUser){
        $user = $this->get_weixin_users_by_openid($fromUser['id']);
        $userData = [
            'subscribe' => !empty($fromUser['subscribe']) ? $fromUser['subscribe'] : "0",
            'openid' => $fromUser['id'],
            'nickname' => !empty($fromUser['nickname']) ? $fromUser['nickname'] : "0",
            'sex' => !empty($fromUser['original']['sex']) ? $fromUser['original']['sex'] : "0",
            'language' => !empty($fromUser['original']['language']) ? $fromUser['original']['language'] : "0",
            'city' => !empty($fromUser['original']['city']) ? $fromUser['original']['city'] : "0",
            'province' => !empty($fromUser['original']['province']) ? $fromUser['original']['province'] : "0",
            'country' => !empty($fromUser['original']['country']) ? $fromUser['original']['country'] : "0",
            'headimgurl' =>!empty($fromUser['original']['headimgurl']) ? $fromUser['original']['headimgurl'] : "0",
            'subscribe_time' => "0",
            'unionid'=>!empty($fromUser['original']['unionid']) ? $fromUser['original']['unionid'] : "0",
        ];
        if (!empty($user)) {
            $this->sava_weixin_users($user['id'],$userData);
        } else {
            $this->db->insert('weixin_users',$userData);
        }
        if($this->db->affected_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有微信用户
     */
    public function get_all_weixin_users(){
        $this->db->select('*');
        $this->db->from('weixin_users');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除微信用户
     */
    public function delete_weixin_users_by($where){
        $this->db->where($where)->delete('weixin_users');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function app_subscribe($fromUser){
        $user = $this->get_weixin_users_by_openid($fromUser['openid']);
        $userData = [
            'subscribe' => !empty($fromUser['subscribe']) ? $fromUser['subscribe'] : "0",
            'openid' => $fromUser['openid'],
            'nickname' => !empty($fromUser['nickname']) ? $fromUser['nickname'] : "0",
            'sex' => !empty($fromUser['sex']) ? $fromUser['sex'] : "0",
            'language' => !empty($fromUser['language']) ? $fromUser['language'] : "0",
            'city' => !empty($fromUser['city']) ? $fromUser['city'] : "0",
            'province' => !empty($fromUser['province']) ? $fromUser['province'] : "0",
            'country' => !empty($fromUser['country']) ? $fromUser['country'] : "0",
            'headimgurl' =>!empty($fromUser['headimgurl']) ? $fromUser['headimgurl'] : "0",
            'subscribe_time' => "0",
            'unionid'=>!empty($fromUser['unionid']) ? $fromUser['unionid'] : "0",
            'from_type'=>1
        ];
        if (!empty($user)) {
            $this->sava_weixin_users($user['id'],$userData);
        } else {
            $this->db->insert('weixin_users',$userData);
        }
        if($this->db->affected_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

    public function update_wechat_users_by_userid($userid,$data){
        $this->db->where('user_id',$userid);
        $this->db->update('weixin_users', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function get_weixin_users_by_uid($uid){
        $query = $this->db
            ->select('*')
            ->get_where('weixin_users', array('user_id'=>$uid));
        return $query->row_array();
    }

    public function first_subscribe($fromUser){
        $user = $this->get_weixin_users_by_openid($fromUser['openid']);
        $userData = [
            'subscribe' => !empty($fromUser['subscribe']) ? $fromUser['subscribe'] : "0",
            'openid' => $fromUser['openid'],
            'nickname' => !empty($fromUser['nickname']) ? $fromUser['nickname'] : "0",
            'sex' => !empty($fromUser['original']['sex']) ? $fromUser['original']['sex'] : "0",
            'language' => !empty($fromUser['original']['language']) ? $fromUser['original']['language'] : "0",
            'city' => !empty($fromUser['original']['city']) ? $fromUser['original']['city'] : "0",
            'province' => !empty($fromUser['original']['province']) ? $fromUser['original']['province'] : "0",
            'country' => !empty($fromUser['original']['country']) ? $fromUser['original']['country'] : "0",
            'headimgurl' =>!empty($fromUser['original']['headimgurl']) ? $fromUser['original']['headimgurl'] : "0",
            'subscribe_time' => "0",
            'unionid'=>!empty($fromUser['original']['unionid']) ? $fromUser['original']['unionid'] : "0",
        ];

        if (!empty($user)) {
            $this->sava_weixin_users($user['id'],$userData);
        } else {
            $this->db->insert('weixin_users',$userData);
        }
        if($this->db->affected_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

    public function get_weixin_users_by_unionid($unionid,$openid = null){

        $this->db->select('*');
        $this->db->from('weixin_users');
        $this->db->where('unionid',$unionid);
        if(!empty($openid)){
            $this->db->where('openid !=',$openid);
        }
        $query = $this->db->get();
        $result = $query->row_array();
        return $result;
    }

    public function refereer_subscribe($fromUser){
        $user = $this->get_weixin_users_by_openid($fromUser['openid']);
        $userData = [
            'subscribe' => !empty($fromUser['subscribe']) ? $fromUser['subscribe'] : "0",
            'openid' => $fromUser['openid'],
            'nickname' => !empty($fromUser['nickname']) ? $fromUser['nickname'] : "0",
            'sex' => !empty($fromUser['sex']) ? $fromUser['sex'] : "0",
            'language' => !empty($fromUser['language']) ? $fromUser['language'] : "0",
            'city' => !empty($fromUser['city']) ? $fromUser['city'] : "0",
            'province' => !empty($fromUser['province']) ? $fromUser['province'] : "0",
            'country' => !empty($fromUser['country']) ? $fromUser['country'] : "0",
            'headimgurl' =>!empty($fromUser['headimgurl']) ? $fromUser['headimgurl'] : "0",
            'subscribe_time' => "0",
            'unionid'=>!empty($fromUser['unionid']) ? $fromUser['unionid'] : "0",
        ];

        if (!empty($user)) {
            $this->sava_weixin_users($user['id'],$userData);
        } else {
            $this->db->insert('weixin_users',$userData);
        }
        if($this->db->affected_rows() > 0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取在微信公众号的openID用户
     * @param $uid
     * @return mixed
     */
    public function get_wx_users_by_uid($uid){

        $this->db->select('*');
        $this->db->from('weixin_users');
        $this->db->where('user_id',$uid);
        $this->db->where('from_type !=',1);
        $query = $this->db->get();
        $result = $query->row_array();
        return $result;
    }
}
